#include "SqlFilter.h"


namespace TCServer
{
    namespace Utility
    {
        QRegularExpression SqlFilter::stringFilter =
            QRegularExpression(R"(\W)",
                QRegularExpression::UseUnicodePropertiesOption | QRegularExpression::OptimizeOnFirstUsageOption);
        QRegularExpression SqlFilter::emailFilter =
            QRegularExpression(R"([^a-zA-Z0-9@._])", 
                QRegularExpression::UseUnicodePropertiesOption | QRegularExpression::OptimizeOnFirstUsageOption);
        QRegularExpression SqlFilter::phoneFilter = 
            QRegularExpression(R"([^0-9-])",
                QRegularExpression::UseUnicodePropertiesOption | QRegularExpression::OptimizeOnFirstUsageOption);

        bool SqlFilter::isValidString(const QString &str)
        {
            stringFilter.optimize();
            return !str.contains(stringFilter);
        }

        bool SqlFilter::isValidEmail(const QString &str)
        {
            return !str.contains(emailFilter);
        }

        bool SqlFilter::isValidPhone(const QString &str)
        {
            return !str.contains(phoneFilter);
        }

        QString SqlFilter::filterEmail(const QString &email, const QString &replacement /*= "_"*/)
        {
            return QString(email).replace(emailFilter, replacement);
        }

        QString SqlFilter::filterPhone(const QString &phone, const QString &replacement /*= "_"*/)
        {
            return QString(phone).replace(phoneFilter, replacement);
        }

        QString SqlFilter::filter(QString str)
        {
            return str;
            //return QString(str).replace(stringFilter, "_");
        }

    }
}